home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / memory / xms200je / hmatest.cpp < prev    next >
C/C++ Source or Header  |  1993-11-22  |  6KB  |  203 lines

  1. //--------------------------------------------------------------------------
  2. //
  3. //      HMATEST.CPP: Test program for HMA class.
  4. //      Copyright (c) J.English 1993.
  5. //      Author's address: je@unix.brighton.ac.uk
  6. //
  7. //      Permission is granted to use copy and distribute the
  8. //      information contained in this file provided that this
  9. //      copyright notice is retained intact and that any software
  10. //      or other document incorporating this file or parts thereof
  11. //      makes the source code for the library of which this file
  12. //      is a part freely available.
  13. //
  14. //--------------------------------------------------------------------------
  15. //
  16. //      Revision history:
  17. //      2.0     Nov 1993        Initial coding
  18. //
  19. //--------------------------------------------------------------------------
  20.  
  21. #include "xms.h"
  22. #include "doserror.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <time.h>
  26.  
  27.  
  28. //--------------------------------------------------------------------------
  29. //
  30. //      Global data.
  31. //
  32. DOSerror e;             // guard against critical errors and control-breaks
  33. HMA* desc;              // pointer to HMA descriptors
  34.  
  35.  
  36. //--------------------------------------------------------------------------
  37. //
  38. //      Function prototypes.
  39. //
  40. char menu ();           // display menu & get user's choice
  41. void allocate ();       // 1. Allocate block
  42. void deallocate ();     // 2. Deallocate block
  43. void copy ();           // 3. Copy block
  44.  
  45. //--------------------------------------------------------------------------
  46. //
  47. //      Main program.
  48. //
  49. //      A menu of choices is displayed, allowing the user to exercise
  50. //      each of the HMA functions available (except HMA to HMA copying).
  51. //
  52. void main ()
  53. {
  54.     char choice;
  55.     for (;;)
  56.     {
  57.         choice = menu ();
  58.         if (choice == 'X')
  59.             break;
  60.  
  61.         switch (choice)
  62.         {
  63.             case '1':
  64.                 allocate ();
  65.                 break;
  66.             case '2':
  67.                 deallocate ();
  68.                 break;
  69.             case '3':
  70.                 copy ();
  71.                 break;
  72.         }
  73.     }
  74.     delete desc;
  75. }
  76.  
  77.  
  78. //--------------------------------------------------------------------------
  79. //
  80. //      Display menu and get user choice.
  81. //
  82. //      The choice must be a line containing a single character from 1 to 3
  83. //      (or X for exit).
  84. //
  85. char menu ()
  86. {
  87.     printf ("\nChoose desired operation:\n"
  88.             "  1) Allocate block\n"
  89.             "  2) Deallocate block\n"
  90.             "  3) Copy block\n"
  91.             "  X) Exit program\n");
  92.     char buff [80];
  93.     for (;;)
  94.     {
  95.         printf ("Enter your choice: ");
  96.         fgets (buff, 80, stdin);
  97.         if ((buff[0] == 'x' || buff[0] == 'X') && buff[1] == '\n')
  98.             return 'X';
  99.         if (buff[0] < '1' || buff[0] > '3' || buff[1] != '\n')
  100.             printf ("Invalid choice!\a\n");
  101.         else
  102.             break;
  103.     }
  104.     printf ("\n");
  105.     return buff[0];
  106. }
  107.  
  108. //--------------------------------------------------------------------------
  109. //
  110. //      Allocate block.
  111. //
  112. //      Attempt to allocate a block of a specified size in the HMA and
  113. //      report the result.
  114. //
  115. void allocate ()
  116. {
  117.     printf ("Enter block size: ");
  118.     unsigned size;
  119.     scanf ("%u", &size);
  120.     while (getchar() != '\n')
  121.         continue;
  122.     desc = new HMA (size);
  123.     printf ("Requested %u bytes, granted %u bytes",
  124.             size, desc->size());
  125.     if (!desc->valid())
  126.     {   printf (" (error code %02X)", !*desc);
  127.         delete desc;
  128.     }
  129.     printf (".\n");
  130. }
  131.  
  132.  
  133. //--------------------------------------------------------------------------
  134. //
  135. //      Deallocate block.
  136. //
  137. //      Attempt to deallocate an existing block and report the result.
  138. //
  139. void deallocate ()
  140. {
  141.     delete desc;
  142.     desc = 0;
  143.     printf ("HMA deallocated.\n");
  144. }
  145.  
  146. //--------------------------------------------------------------------------
  147. //
  148. //      Copy to/from HMA block.
  149. //
  150. //      Prompts for a block number.  An array of random numbers of the
  151. //      specified size is created in conventional memory, copied to the
  152. //      specified block starting at the specified offset, copied back to
  153. //      conventional memory, and finally compared with the original block.
  154. //
  155. void copy ()
  156. {
  157.     unsigned i;
  158.     
  159.     // transfer size s, offset offset
  160.     printf ("Enter transfer size: ");
  161.     unsigned s;
  162.     scanf ("%u", &s);
  163.     while (getchar() != '\n')
  164.         { ; }
  165.     printf ("Enter HMA offset:    ");
  166.     unsigned offset;
  167.     scanf ("%u", &offset);
  168.     while (getchar() != '\n')
  169.         { ; }
  170.  
  171.     char* x = new char [s];
  172.     char* y = new char [s];
  173.     if (x == 0 || y == 0)
  174.     {   printf ("Not enough real memory!\n");
  175.         return;
  176.     }
  177.  
  178.     printf ("Copying %u bytes to/from HMA offset %u.\n", s, offset);
  179.     printf ("Generating random data... "); fflush (stdout);
  180.     randomize ();
  181.     for (i = 0; i < s; i++)
  182.         x[i] = random (256);
  183.     printf ("done.\n");
  184.  
  185.     printf ("Copying random data to HMA... "); fflush (stdout);
  186.     printf ("copied %u bytes.\n", HMA::copy (desc->at(offset), x, s));
  187.  
  188.     printf ("Copying data back from HMA... "); fflush (stdout);
  189.     printf ("copied %u bytes.\n", (s = HMA::copy (y, desc->at(offset), s)));
  190.  
  191.     for (i = 0; i < s; i++)
  192.     {   if (x[i] != y[i])
  193.         {   printf ("Verification error at offset %u.\n", i);
  194.             break;
  195.         }
  196.     }
  197.     if (i == s)
  198.         printf ("Copy verified successfully.\n");
  199.  
  200.     delete x;
  201.     delete y;
  202. }
  203.